/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import puertos.Communicator;
import puertos.Paralelo;

/**
 *
 * @author innovapps.co
 */
public class Principal extends javax.swing.JFrame {

    public TraccionForm ensayo;
    public SoldaduraForm soldadura;
    public AlambreForm alambre;
    
    Communicator communicator = null;
    Paralelo paralelo = null;

    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
        jPanelPrincipal.setVisible(true);

        //Inicializar Tracción Form
        ensayo = new TraccionForm(this);
        ensayo.setSize(getContentPane().getSize());
        getContentPane().add(ensayo);
        ensayo.setVisible(false);

        //Inicializar Soldadura Form
        soldadura = new SoldaduraForm(this);
        soldadura.setSize(getContentPane().getSize());
        getContentPane().add(soldadura);
        soldadura.setVisible(false);
        
        //Inicializar Alambre Form
        alambre = new AlambreForm(this);
        alambre.setSize(getContentPane().getSize());
        getContentPane().add(alambre);
        alambre.setVisible(false);
        
        //Inicializar comunicación con los puertos
        communicator = new Communicator(this);
        paralelo = new Paralelo(this);
        communicator.searchForPorts();
        paralelo.searchForPorts();
        paralelo.escribirEnPuerto((byte) 0, (String) ensayo.cboxPorts1.getSelectedItem());
       
        pack();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TraccionJPAPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("TraccionJPAPU").createEntityManager();
        ensayoQuery = java.beans.Beans.isDesignTime() ? null : TraccionJPAPUEntityManager.createQuery("SELECT e FROM Ensayo e");
        ensayoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : ensayoQuery.getResultList();
        jPanelPrincipal = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuArchivo = new javax.swing.JMenu();
        jMenuItemSalir = new javax.swing.JMenuItem();
        jMenuEditar = new javax.swing.JMenu();
        jMenuItemConfiguracion = new javax.swing.JMenuItem();
        jMenuEnsayos = new javax.swing.JMenu();
        jMenuItemNewEnsayoTraccion = new javax.swing.JMenuItem();
        corteAlambre = new javax.swing.JMenuItem();
        jMenuItemSoldadura = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ensayo Tracción");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanelPrincipal.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/logoApp.png"))); // NOI18N

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap(411, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(395, 395, 395))
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addGap(175, 175, 175)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(280, Short.MAX_VALUE))
        );

        jMenuArchivo.setText("Archivo");

        jMenuItemSalir.setText("Salir");
        jMenuItemSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSalirActionPerformed(evt);
            }
        });
        jMenuArchivo.add(jMenuItemSalir);

        jMenuBar1.add(jMenuArchivo);

        jMenuEditar.setText("Editar");

        jMenuItemConfiguracion.setText("Configuración");
        jMenuItemConfiguracion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemConfiguracionActionPerformed(evt);
            }
        });
        jMenuEditar.add(jMenuItemConfiguracion);

        jMenuBar1.add(jMenuEditar);

        jMenuEnsayos.setText("Ensayos");

        jMenuItemNewEnsayoTraccion.setText("Resistencia a la tracción");
        jMenuItemNewEnsayoTraccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewEnsayoTraccionActionPerformed(evt);
            }
        });
        jMenuEnsayos.add(jMenuItemNewEnsayoTraccion);

        corteAlambre.setText("Corte de Alambre");
        corteAlambre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                corteAlambreActionPerformed(evt);
            }
        });
        jMenuEnsayos.add(corteAlambre);

        jMenuItemSoldadura.setText("Soldadura");
        jMenuItemSoldadura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSoldaduraActionPerformed(evt);
            }
        });
        jMenuEnsayos.add(jMenuItemSoldadura);

        jMenuBar1.add(jMenuEnsayos);

        jMenu4.setText("Ayuda");
        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSalirActionPerformed
        communicator.disconnect();
        dispose();
    }//GEN-LAST:event_jMenuItemSalirActionPerformed

    private void jMenuItemConfiguracionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemConfiguracionActionPerformed
        new Configuracion(this).setVisible(true);
    }//GEN-LAST:event_jMenuItemConfiguracionActionPerformed

    private void jMenuItemNewEnsayoTraccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewEnsayoTraccionActionPerformed
        jPanelPrincipal.setVisible(false);
        soldadura.actualizar();
        soldadura.setVisible(false);
        alambre.actualizar();
        alambre.setVisible(false);
        ensayo.actualizar();
        ensayo.nuevoEnsayo();
        ensayo.setVisible(true);
    }//GEN-LAST:event_jMenuItemNewEnsayoTraccionActionPerformed

    private void corteAlambreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_corteAlambreActionPerformed
        jPanelPrincipal.setVisible(false);
        soldadura.actualizar();
        soldadura.setVisible(false);
        ensayo.actualizar();
        ensayo.setVisible(false);
        alambre.actualizar();
        alambre.nuevoEnsayo();
        alambre.setVisible(true);        
    }//GEN-LAST:event_corteAlambreActionPerformed

    private void jMenuItemSoldaduraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSoldaduraActionPerformed
       jPanelPrincipal.setVisible(false);
        ensayo.actualizar();
        ensayo.setVisible(false);
        alambre.actualizar();
        alambre.setVisible(false);
        soldadura.actualizar();
        soldadura.nuevo();
        soldadura.setVisible(true);
    }//GEN-LAST:event_jMenuItemSoldaduraActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        communicator.disconnect();
    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager TraccionJPAPUEntityManager;
    private javax.swing.JMenuItem corteAlambre;
    private java.util.List<jpa.entities.Ensayo> ensayoList;
    private javax.persistence.Query ensayoQuery;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenuArchivo;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuEditar;
    private javax.swing.JMenu jMenuEnsayos;
    private javax.swing.JMenuItem jMenuItemConfiguracion;
    private javax.swing.JMenuItem jMenuItemNewEnsayoTraccion;
    private javax.swing.JMenuItem jMenuItemSalir;
    private javax.swing.JMenuItem jMenuItemSoldadura;
    private javax.swing.JPanel jPanelPrincipal;
    // End of variables declaration//GEN-END:variables
}
